/******************************************
 * Author: Dave Spadea, Spadea Enterprises, Inc.
 * Date: November 2010
 * 
 * This code is released by Spadea Enterprises, Inc for general consumption 
 * under the Apache Software License.
 * 
 */

package net.spadea.framework.service;

import java.util.ArrayList;
import org.slf4j.*;

public abstract class ServiceBase implements Runnable {

	private static ArrayList<Thread> threads = new ArrayList<Thread>();
	public static Logger log; 
	
	public ServiceBase() {
		log = LoggerFactory.getLogger(this.getClass().getSimpleName());
	}
	
	/** 
	 * 
	 * @param workers
	 * 
	 * After instantiating the derived class and setting any necessary configurations,
	 * run start_workers, passing in the number of worker threads which should be started.
	 * 
	 */
	
	public void start_workers(int workers) {
		
		for (int i = 0; i < workers; i++) {
			Thread t = new Thread(this);
			t.start();
			threads.add(t);
		}
	}
	
	public void wait_for_workers() {
		for (Thread t : threads) {
			try {
				t.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	

	/** run 
	 * 
	 * Override this to provide an entry point in your service. It will be invoked in each thread.
	 * A new class is NOT instantiated for each thread, so instance data is shared.
	 * 
	 * **/
	public abstract void run();
}
